<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Feed_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
	}
	
	function clearCacheTeam(){
		$this->feed->clearTeamsCache();
		exit();
	} 
	
	function match_submit(){
		$TOURNAMENT = $this->tournament_entity->init('id', $_POST['tournament_id']);
		$dateTimeStart = sysDateTimeFormat($TOURNAMENT->getStartTime());
		$dateTimeEnd = sysDateTimeFormat($TOURNAMENT->getEndTime());
		
		$matchArray = $this->match_entity->getAllMatches();
		$arrayMatchFeedId = array();
		foreach($matchArray as $item){
			if($item->feed_match_id){
				$arrayMatchFeedId[] = $item->feed_match_id;
			}
		}
		
		$matchFeedIdPost = $this->input->post('feed_match_id',0);
		if(in_array($matchFeedIdPost , $arrayMatchFeedId)){
			echo "This Match had existed in system.Can not add anymore.";
			exit();
		}
		
		$startTime =  convertToTimeStampFromFormat($_POST['start_time']);
		$endTime = ($TOURNAMENT->getEndTime() - 10);
		$convertionRate = $_POST['convertion_rate'];
		$convertionEnable = !isset($_POST['convertion_enable']) ? 'no':'yes';
		
		$pcArr = explode(':',$convertionRate);
		$convertionRate =  trim( $pcArr[0] ) .":". trim( $pcArr[1] ) ;
		
		if( !isset($pcArr[0]) OR ! $this->phpvalidator->is_natural($pcArr[0]) OR $pcArr[0]< 0 ){
			echo "Convertion rate is not valid.";
			exit();
		}
		
		if( !isset($pcArr[1]) OR ! $this->phpvalidator->is_natural($pcArr[1]) OR $pcArr[1]< 0 ){
			echo "Convertion rate is not valid.";
			exit();
		}
		
		if($startTime == 0 OR $endTime== 0 ){
			echo "Datetime not validated.";exit();
		}
		if($startTime < $TOURNAMENT->getStartTime() ){
			echo "Start time not validated. (&lt; {$dateTimeStart})";exit();
		}
		if($endTime > $TOURNAMENT->getEndTime()){
			echo "End time not validated. (&gt; {$dateTimeEnd})";exit();
		}
		if($endTime <= time()){
			echo "End time not validated. (Can not < current time)";exit();
		}
		if($_POST['away'] == $_POST['home']){
			echo "Home and Away can not duplicate.";exit();
		}
	/*	if($_POST['home_score']){
			if(! $this->phpvalidator->is_natural($_POST['home_score']) OR $_POST['home_score'] < 0){
				echo "Home score is not valid.";
				exit();
			}
		}
		if($_POST['home_wickets']){
			if(! $this->phpvalidator->is_natural($_POST['home_wickets']) OR $_POST['home_wickets'] < 0){
				echo "Home wickets is not valid.";
				exit();
			}
		}
		if($_POST['home_overs']){
			if(! $this->phpvalidator->is_numeric($_POST['home_overs']) OR $_POST['home_overs'] < 0){
				echo "Home overs is not valid.";
				exit();
			}
		}
	*/	
	/*	if(! $this->phpvalidator->is_natural($_POST['home_runs']) OR $_POST['home_runs'] < 0){
			echo "Home runs is not valid.";
			exit();
		}	*/
	/*	if($_POST['away_score']){
			if(! $this->phpvalidator->is_natural($_POST['away_score']) OR $_POST['away_score'] < 0){
				echo "Away score is not valid.";
				exit();
			}
		}
		if($_POST['away_wickets']){
			if(! $this->phpvalidator->is_natural($_POST['away_wickets']) OR $_POST['away_wickets'] < 0){
				echo "Away wickets is not valid.";
				exit();
			}
		}
		if($_POST['away_overs']){
			if(! $this->phpvalidator->is_numeric($_POST['away_overs']) OR $_POST['away_overs'] < 0){
				echo "Away overs is not valid.";
				exit();
			}
		}
	*/	
	/*	if(! $this->phpvalidator->is_natural($_POST['away_runs']) OR $_POST['away_runs'] < 0){
			echo "Away runs is not valid.";
			exit();
		}	*/
		
		$sport_id = intval($_POST['sport_id']);
		//if sport_id = 1: Test cricket: DRAWN | other: NOT DRAWN
		
		$newId = $this->match_entity->createNewMatchEntity();
		$MATCH = $this->match_entity->init('id', $newId);
		
		$MATCH->setHome($_POST['home']);
		$MATCH->setHomeScore(strlen($_POST['home_score']) ? $_POST['home_score'] : '');
		$MATCH->setHomeWickets(strlen($_POST['home_wickets']) ? $_POST['home_wickets']: '');
		$MATCH->setHomeOvers(strlen($_POST['home_overs']) ? $_POST['home_overs']: '');
	//	$MATCH->setHomeRuns($_POST['home_runs']);
		$MATCH->setAway($_POST['away']);
		$MATCH->setAwayScore(strlen($_POST['away_score']) ? $_POST['away_score']: '');
		$MATCH->setAwayWickets(strlen($_POST['away_wickets']) ? $_POST['away_wickets']: '');
		$MATCH->setAwayOvers(strlen($_POST['away_overs']) ? $_POST['away_overs']: '');
	//	$MATCH->setAwayRuns($_POST['away_runs']);
		$MATCH->setResult($_POST['result']);
		$MATCH->setStartTime($startTime);
		$MATCH->setEndTime($endTime);
		$MATCH->setBetEnable('yes');
		$MATCH->setTournamentId($_POST['tournament_id']);
		$MATCH->setConvertionRate($convertionRate);
		$MATCH->setEnableConvertion($convertionEnable);
		$MATCH->setEmailNotifyEnable($this->input->post('email_notify_enable',NULL));
		$MATCH->setSmsNotifyEnable($this->input->post('sms_notify_enable',NULL));
		
		$MATCH->setFeedType($this->input->post('feed_type',NULL));
		$MATCH->setFeedKey($this->input->post('feed_key',NULL));
		$MATCH->setFeedMatchId($this->input->post('feed_match_id',NULL));
		$MATCH->setFeedCategoryId($this->input->post('feed_category_id',NULL));
		$MATCH->setTitle($this->input->post('title',NULL));
		
		if($startTime > time()){
			$status = 'upcoming';
		}else{
			$status = 'living';
		}
		$MATCH->setStatus($status);
		$MATCH->setSportId($sport_id);
		
		$MATCH->update();
		
		$event_category_id = $this->event_category_entity->createNewEventCategoryEntity($MATCH->getId());
		$EVENT_CATEGORY = $this->event_category_entity->init('id', $event_category_id);
		$EVENT_CATEGORY->setCategory("Who will win the match");
		$EVENT_CATEGORY->setMatchId($newId);
		$EVENT_CATEGORY->setStatus('living');
		$EVENT_CATEGORY->setIsRoot('yes');
		$EVENT_CATEGORY->update();
		
		$HOME_TEAM = clone_($this->team_entity->init('id',$_POST['home']));
		$AWAY_TEAM = $this->team_entity->init('id',$_POST['away']);
		
		$event_id = $this->event_entity->createNewEventEntity();
		$EVENT = $this->event_entity->init('id', $event_id);
		$EVENT->setName($HOME_TEAM->getName());
		$EVENT->setTeamId($HOME_TEAM->getId());
		$EVENT->setMatchId($newId);
		$EVENT->setStatus('living');
		$EVENT->setEventCategoryId($event_category_id);
		$EVENT->setStartTime($startTime);
		$EVENT->setEndTime($endTime);
		$EVENT->setBetEnable('yes');
		$EVENT->setOdds(1);
		$EVENT->setIsRoot('yes');
		$EVENT->update();
		
		if($sport_id == 1){
			$EVENT_CATEGORY->setCategory("Match Result");
			$EVENT_CATEGORY->update();
			
			$event_id = $this->event_entity->createNewEventEntity();
			$EVENT = $this->event_entity->init('id', $event_id);
			$EVENT->setName("Draw");
			$EVENT->setTeamId(0);
			$EVENT->setMatchId($newId);
			$EVENT->setStatus('living');
			$EVENT->setEventCategoryId($event_category_id);
			$EVENT->setStartTime($startTime);
			$EVENT->setEndTime($endTime);
			$EVENT->setBetEnable('yes');
			$EVENT->setOdds(1);
			$EVENT->setIsRoot('yes');
			$EVENT->update();
		}
		
		$event_id = $this->event_entity->createNewEventEntity();
		$EVENT = $this->event_entity->init('id', $event_id);
		$EVENT->setName($AWAY_TEAM->getName());
		$EVENT->setTeamId($AWAY_TEAM->getId());
		$EVENT->setMatchId($newId);
		$EVENT->setStatus('living');
		$EVENT->setEventCategoryId($event_category_id);
		$EVENT->setStartTime($startTime);
		$EVENT->setEndTime($endTime);
		$EVENT->setBetEnable('yes');
		$EVENT->setOdds(1);
		$EVENT->setIsRoot('yes');
		$EVENT->update();
		
		if($this->input->post('use_default_event',TRUE) == 'yes'){ $this->add_default_event_to_match(0,$MATCH->getId(),$sport_id );}
	
		$event_category_id = $this->event_category_entity->createNewEventCategoryEntity($MATCH->getId());
		$EVENT_CATEGORY = $this->event_category_entity->init('id', $event_category_id);
		$EVENT_CATEGORY->setCategory("Who will win the Toss?");
		$EVENT_CATEGORY->setMatchId($newId);
		$EVENT_CATEGORY->setStatus('living');
		$EVENT_CATEGORY->setIsRoot(NULL);
		$EVENT_CATEGORY->update();
		 
		$event_id = $this->event_entity->createNewEventEntity();
		$EVENT = $this->event_entity->init('id', $event_id);
		$EVENT->setName($HOME_TEAM->getName());
		$EVENT->setTeamId($HOME_TEAM->getId());
		$EVENT->setMatchId($newId);
		$EVENT->setStatus('living');
		$EVENT->setEventCategoryId($event_category_id);
		$EVENT->setStartTime($startTime);
		$EVENT->setEndTime($endTime);
		$EVENT->setBetEnable('yes');
		$EVENT->setOdds(1);
		$EVENT->setIsRoot(NULL);
		$EVENT->update();
		
		$event_id = $this->event_entity->createNewEventEntity();
		$EVENT = $this->event_entity->init('id', $event_id);
		$EVENT->setName($AWAY_TEAM->getName());
		$EVENT->setTeamId($AWAY_TEAM->getId());
		$EVENT->setMatchId($newId);
		$EVENT->setStatus('living');
		$EVENT->setEventCategoryId($event_category_id);
		$EVENT->setStartTime($startTime);
		$EVENT->setEndTime($endTime);
		$EVENT->setBetEnable('yes');
		$EVENT->setOdds(1);
		$EVENT->setIsRoot(NULL);
		$EVENT->update();
		
		if($sport_id == 1){
			$event_category_id = $this->event_category_entity->createNewEventCategoryEntity($MATCH->getId());
			$EVENT_CATEGORY = $this->event_category_entity->init('id', $event_category_id);
			$EVENT_CATEGORY->setCategory("Who will take the 1st innings Lead?");
			$EVENT_CATEGORY->setMatchId($newId);
			$EVENT_CATEGORY->setStatus('living');
			$EVENT_CATEGORY->setIsRoot(NULL);
			$EVENT_CATEGORY->update();
			 
			$event_id = $this->event_entity->createNewEventEntity();
			$EVENT = $this->event_entity->init('id', $event_id);
			$EVENT->setName($HOME_TEAM->getName());
			$EVENT->setTeamId($HOME_TEAM->getId());
			$EVENT->setMatchId($newId);
			$EVENT->setStatus('living');
			$EVENT->setEventCategoryId($event_category_id);
			$EVENT->setStartTime($startTime);
			$EVENT->setEndTime($endTime);
			$EVENT->setBetEnable('yes');
			$EVENT->setOdds(1);
			$EVENT->setIsRoot(NULL);
			$EVENT->update();
			
			$event_id = $this->event_entity->createNewEventEntity();
			$EVENT = $this->event_entity->init('id', $event_id);
			$EVENT->setName($AWAY_TEAM->getName());
			$EVENT->setTeamId($AWAY_TEAM->getId());
			$EVENT->setMatchId($newId);
			$EVENT->setStatus('living');
			$EVENT->setEventCategoryId($event_category_id);
			$EVENT->setStartTime($startTime);
			$EVENT->setEndTime($endTime);
			$EVENT->setBetEnable('yes');
			$EVENT->setOdds(1);
			$EVENT->setIsRoot(NULL);
			$EVENT->update();
		}
		 
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo "ok";
		exit();
	}
	
	function add_default_event_to_match($team_id, $match_id,$sport_id){
		$MATCH = $this->match_entity->init('id',$match_id);
		$HOME = clone_($this->team_entity->init('id',$MATCH->getHome()));
		$AWAY = clone_($this->team_entity->init('id',$MATCH->getAway()));
		
		$start_time = $MATCH->getStartTime();
		$end_time = $MATCH->getEndTime();
		
		$category_array = $this->pre_define_entity->getListCategoriesBySportId($sport_id);
		foreach($category_array as $cat){
			$event_category_id = $this->event_category_entity->createNewEventCategoryEntity($match_id);
			$EVENT_CATEGORY = $this->event_category_entity->init('id', $event_category_id);
			$EVENT_CATEGORY->setCategory($cat->title);
			$EVENT_CATEGORY->setMatchId($match_id);
			$EVENT_CATEGORY->setStatus('living');
			$EVENT_CATEGORY->setIsRoot(NULL);
			$EVENT_CATEGORY->update();
			
			$event_array = $this->pre_define_entity->getListEventOfCategory($cat->id);
			foreach($event_array as $ev){
				$event_id = $this->event_entity->createNewEventEntity();
				$EVENT = $this->event_entity->init('id', $event_id);
				$event_name = str_replace( array('{###HOME###}', '{###AWAY###}'), array( $HOME->getName(), $AWAY->getName()), $ev->title);
				
				$EVENT->setName($event_name);
				$EVENT->setTeamId($team_id);
				$EVENT->setMatchId($match_id);
				$EVENT->setStatus('living');
				$EVENT->setEventCategoryId($event_category_id);
				//$EVENT->setStartTime($start_time);
				//$EVENT->setEndTime($end_time);
				$EVENT->setBetEnable('yes');
				$EVENT->setOdds($ev->odds);
				$EVENT->setIsRoot(NULL);
				$EVENT->update();
			}
		}
	}
	
//end class	
}	